<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>4.4 10!</title>
</head>

<body>
    <script>
        // 方案一
        var sum1 = 0;
        for (var i = 1; i <= 10; i++) {
            var n = 1;
            // console.log('iiii:' + i);
            for (var j = 1; j <= i; j++) {
                // console.log('j:' + j);
                n *= j;
            }
            sum1 += n;
        }
        alert(" 1!+2!+3!+…..＋10！=" + sum1);

        // 方案二
        function factorial(n) {
            if ((n == 0) || (n == 1))
                return 1;
            else
                return (n * factorial(n - 1));
        }
        var sum2 = 0;
        for (var i = 1; i <= 10; i++) {
            sum2 += factorial(i);
        }
        alert(sum2);

        // 方案三
        var sum_fac, fac, i;
        sum_fac = 0;
        fac = 1;
        for (i = 1; i <= 10; i++) {
            // 1*1*2*3*4*5*6*7*8*9*10
            fac *= i;
            sum_fac += fac;
        }
        alert("1! + 2! + 3! + ... + 10!=" + sum_fac);
    </script>
</body>

</html>